﻿using System;
using System.ComponentModel;
using System.Xml;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.ServiceHelper;
using Kingdee.BOS.Util;

namespace Caraya.MFG.BusinessPlugIn
{
    [Description("二开付款单插件"), HotUpdate]
    public class ApPayBillEdit : AbstractDynamicFormPlugIn
    {
        public override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            var lgPrdOrgId = this.Context.CurrentOrganizationInfo.ID.ToString();
            var userId = this.Context.UserId.ToString();

            //var sql1 = "SELECT FPARAMETERS FROM T_BAS_USERPARAMETER where FPARAMETEROBJID='AP_PAYBILL' and FUSERID=" + userId;

            //var xml = DBServiceHelper.ExecuteScalar(this.Context, sql1, "");

            string xml = UserParamterServiceHelper.Load(base.Context, "AP_PAYBILL", base.Context.UserId, "UserParameter");
            //var xml = UserParamterServiceHelper.LoadUserOptionParameter(this.Context, "AP_PAYBILL", this.Context.UserId);
            if (IsNodeBool(xml))
            {
                var fdate = this.Model.GetValue("FDate");
                var sql = "SELECT TOP 1 FDATE FROM T_AP_PAYBILL WHERE FMODIFIERID = " + this.Context.UserId + " AND FPAYORGID = " + lgPrdOrgId + " ORDER BY FID DESC";
                var curDate = DBServiceHelper.ExecuteScalar(this.Context, sql, fdate);
                this.Model.SetValue("FDATE", curDate);
            }
        }

        private bool IsNodeBool(string xmlStr = "")
        {
            try
            {
                if (string.IsNullOrWhiteSpace(xmlStr))
                {
                    return false;
                }
                else
                {
                    var xml = new XmlDocument();
                    xml.LoadXml(xmlStr);
                    var node = xml.SelectSingleNode("/BillUserParameter/F_CARA_CheckBox1");
                    if (node != null)
                    {
                        return bool.Parse(node.InnerText);
                    }
                    else { return false; }
                }
            }
            catch
            {
                return false;
            }
        }
    }
}
